package sg.edu.nus.iss.umpire.email;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import sg.edu.nus.iss.umpire.support.AppConstants;

public class MailService {
	  private static Logger log = Logger.getLogger(MailService.class.getCanonicalName());

	  //mail id from which the mail has to be sent
	
	  public void send(String toAddress, String subject, String msgBody)
		      throws IOException {
		  Properties props = new Properties();
		  Session session = Session.getDefaultInstance(props, null);

		  try {
		    Message msg = new MimeMessage(session);
		    msg.setFrom(new InternetAddress(AppConstants.ADMIN_EMAIL));
		    InternetAddress to = new InternetAddress(toAddress);
		    msg.addRecipient(Message.RecipientType.TO, to);
		    msg.setSubject(subject);
		    //msg.setText(msgBody);
		    msg.setContent(msgBody, "text/plain");
		    Transport.send(msg, new InternetAddress[] { to });
		  } catch (AddressException addressException) {
		    log.log(Level.SEVERE, "Address Exception , mail could not be sent", addressException);
		  } catch (MessagingException messageException) {
		    log.log(Level.SEVERE, "Messaging Exception , mail could not be sent", messageException);
		  }
	  }
}
